package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import cn.com.kh.signUtil
import com.alibaba.fastjson.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
import groovy.json.JsonSlurper

import java.text.SimpleDateFormat


/**
 * Html文件处理API
 */

public class apiHtmlDeal implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(apiHtmlDeal.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");

        // 初始化返回参数
        JSONObject result = new JSONObject();
        JSONObject data = new JSONObject();

        JSONObject resultJson = null;
        //接收客户端调用参数
        String callJson = "";
        InputStreamReader reader = new InputStreamReader(request.getInputStream(), "UTF-8");
        BufferedReader iny = new BufferedReader(reader);
        String line;
        while ((line = iny.readLine()) != null) {
            callJson += line;
        }

        log.info("[调用请求信息]:" + callJson);

        try {
            //解析Json
            JSONObject inJson = JSONObject.parseObject(callJson);

            String in_objective_guid = inJson.getJSONObject("data").getString("in_objective_guid");
            String in_map_id = inJson.getJSONObject("data").getString("in_map_id");
            log.debug("解析参数信息:"+" in_objective_guid:"+ in_objective_guid);

            String in_pathname="";
            String addsinglenode="";//定义需要拼接的节点
            String checknode=""; //定义开始拼接节点

            if(in_map_id=="58Q0KO"){ //租赁
                in_pathname="printwotemplate.html";
                addsinglenode = '''<tr><td align="center" >批阅信息</td><td colspan="5" class=node>checkinfo</td></tr>''';
                checknode = '''<tr><td align="center" >批阅信息</td><td colspan="5" class=node>checkinfo</td></tr>''';
            }else if(in_map_id=="41EZT8"){//长租退水电
                in_pathname="PrintLongRetreat.html";
                addsinglenode = '''<tr><td align="center" >批阅信息</td><td colspan="3" class=node>checkinfo</td></tr>''';
                checknode = '''<tr><td align="center" >批阅信息</td><td colspan="3" class=node>checkinfo</td></tr>''';
            }else if(in_map_id=="U362C6"){// 车辆IC卡
                in_pathname="PrintICCard.html";
                addsinglenode = '''<tr><td align="center" >批阅信息</td><td colspan="6" class=node>checkinfo</td></tr>''';
                checknode = '''<tr><td align="center" >批阅信息</td><td colspan="6" class=node>checkinfo</td></tr>''';

                //20211116Tang改
            }else if(in_map_id == 'ZALLDU'){  //铺位装修审批
                in_pathname="PrintShopdecorationacc.html";
                addsinglenode = '''<tr><td align="center" >批阅信息</td><td colspan="5" class=node>checkinfo</td></tr>''';
                checknode = '''<tr><td >批阅信息</td><td colspan="5" class=node>checkinfo</td></tr>''';
            }else if(in_map_id == 'ANYNQC'){  //档位退租退压
                in_pathname="PrintStallCancellation.html";
                addsinglenode = '''<tr><td align="center" >批阅信息</td><td colspan="5" class=node>checkinfo</td></tr>''';
                checknode = '''<tr><td >批阅信息</td><td colspan="5" class=node>checkinfo</td></tr>''';
            }else if(in_map_id == 'AQ43MB'){ //长租水电押金
                in_pathname="PrintLongWater_ele.html";
                addsinglenode = '''<tr><td >批阅信息</td><td colspan="6" class=node>checkinfo</td></tr>''';
                checknode = '''<tr><td >批阅信息</td><td colspan="6" class=node>checkinfo</td></tr>''';
            }else if(in_map_id == 'S7ILT1'){ //临时档位租赁
                in_pathname="PrintTemporaryGear.html";
                addsinglenode = '''<tr><td >批阅信息</td><td colspan="6" class=node>checkinfo</td></tr>''';
                checknode = '''<tr><td >批阅信息</td><td colspan="6" class=node>checkinfo</td></tr>''';
            }else if(in_map_id == 'WAEVFI'){ // 临时档位退租
                in_pathname="PrintTemporarySurrender.html";
                addsinglenode = '''<tr><td >批阅信息</td><td colspan="7" class=node>checkinfo</td></tr>''';
                checknode = '''<tr><td >批阅信息</td><td colspan="7" class=node>checkinfo</td></tr>''';
            }else if(in_map_id == 'KHZM6E'){ // 临时档位退租
                in_pathname="PrintBulkTransfer.html";
                addsinglenode = '''<tr><td >批阅信息</td><td colspan="5" class=node>checkinfo</td></tr>''';
                checknode = '''<tr><td >批阅信息</td><td colspan="5" class=node>checkinfo</td></tr>''';
            }else if(in_map_id == 'WUL3QJ'){
                in_pathname="PrintBUlkOrder.html";
                addsinglenode = '''<tr><td >批阅信息</td><td colspan="8" class=node>checkinfo</td></tr>''';
                checknode = '''<tr><td >批阅信息</td><td colspan="8" class=node>checkinfo</td></tr>''';
            }


            //末尾拼接
            String endnode = '''</table></div></body></html>'''

            //查询工单处理步骤内容
            String queryWorkOrderSQL = '''
             select distinct wfm.mapname,wom.nodemapname as dealnode,wos.comments as dealresults,
             CONCAT('【',swos.workorderstepstatusdesc,'】 ','[',if(s.staffname is null,cm.customername,s.staffname),' ',wos.modifieddate,']') as dealstaff,
             obj.map_id as map_id,wos.workorderstepstatus,cm.customername,s.staffname
              from objective obj
              left join workorderstep3 wos on obj.objective_guid = wos.workorder_guid
              left join rolegroup r on wos.oldhandlegroup_id = r.rolegroup_id
              left join staff s on wos.oldhandleby_id = s.staff_id 
              left join sysc_workorderstepstatus swos on wos.workorderstepstatus = swos.workorderstepstatus_id
              left join wo3_frame_map wfm on obj.map_id = wfm.map_id
              left join wo3_node_map wom on obj.map_id = wom.map_id and wos.from_stepidmap = wom.nodemap_id 
              left join customer cm on obj.CUSTOMER_GUID = cm.CUSTOMER_GUID ''' + "\n" +
                    "where wos.oldhandlegroup_id != 'IB0001' and wos.oldhandleby_id !='' and wos.oldhandleby_id is not null and obj.objective_guid ='" + in_objective_guid + "' " + "\n" +
                    "order by wos.modifieddate ";

            log.debug("执行任务数据查询SQL:" + queryWorkOrderSQL);
            List WorkOrderList = eliteBean.chaxun_sql(queryWorkOrderSQL, "dataSource");
            //获取总行数
            log.debug("执行工单查询数据SQL获取总条数:" + WorkOrderList.size());

            String addnodes = "";
            String adddealnode = "";
            String ContractTitle = "";
            String map_id="";
            for (int i = 0; i < WorkOrderList.size(); i++) {
                Map selectResultcm = WorkOrderList.get(i);
                String MapName = selectResultcm.get("mapname");
                String DealNode = selectResultcm.get("dealnode");
                String DealResults = selectResultcm.get("dealresults");
                String DealStaff = selectResultcm.get("dealstaff");
                map_id = selectResultcm.get("map_id");
                String DealInfo = DealStaff + "\n\t" + DealResults;

                log.debug(DealNode)
                log.debug(DealInfo)
                adddealnode = addsinglenode.replaceAll("批阅信息",DealNode);
                adddealnode = adddealnode.replaceAll("checkinfo",DealInfo);

                addnodes = addnodes + adddealnode;
                ContractTitle = MapName;
            }

            log.debug("拼接后替换内容："+addnodes);
            //打开模板文件准备处理
            String ReHtml = '';

            File file1 = new File("/data/apache-tomcat-dabc/webapps/ngsapitask/print/template/"+in_pathname);
            def Linelist = file1.readLines()
            Linelist.find() {

                    if (it.trim()==checknode){
                        log.debug("开始截断拼接报文："+ it.trim());
                        ReHtml = ReHtml + addnodes + endnode;
                        return true;
                }
                ReHtml = ReHtml + it + "\n";
                return false;
            }

            //查询工单合同信息
            String queryContractInfoSQL = '''select 
                                                    case when s.staffname is null or s.staffname = '' then '' else s.staffname end AS acceptor,
                                                    case when r.rolegroupdescription is null or r.rolegroupdescription = '' then '' else r.rolegroupdescription end AS rolegroupdescription,
                                                    case when t.writedate is null or t.writedate = '' then '' else t.writedate end AS createddate,
                                                    case when t.writedate is null or t.writedate = '' then '' else DATE_FORMAT(t.writedate,'%Y年%m月%d日') end AS writedate,
                                                    case when t.undertaker is null or t.undertaker = '' then '' else t.undertaker end AS htcbr,
                                                    case when t.undertaker_mobile is null or t.undertaker_mobile = '' then '' else t.undertaker_mobile end AS cbrdh,
                                                    case when t.contract_title is null or t.contract_title = '' then '' else t.contract_title end AS contract_name,
                                                    case when t.lease_term is null or t.lease_term = '' then '' else DATE_FORMAT(t.lease_term,'%Y年%m月%d日') end AS lease_term,
                                                    case when t.lease_term_end is null or t.lease_term_end = '' then '' else DATE_FORMAT(t.lease_term_end,'%Y年%m月%d日') end AS lease_term_end,
                                                    case when t.tenant is null or t.tenant = '' then '' else t.tenant end as tenant,
                                                    case when t.tenant_mobile is null or t.tenant_mobile = '' then '' else t.tenant_mobile end as tenant_mobile,
                                                    case when t.tenant_code is null or t.tenant_code = '' then '' else t.tenant_code end as tenant_code,
                                                    case when t.stalls_code is null or t.stalls_code = '' then '' else t.stalls_code end as stalls_code,
                                                    case when t.area is null or t.area = '' then '0' else t.area end as area,
                                                    case when t.monthly_rent is null or t.monthly_rent = '' then '0' else t.monthly_rent end AS myzj,
                                                    case when t.monthly_management_fee is null or t.monthly_management_fee = '' then '0' else t.monthly_management_fee end AS myglf,
                                                    case when t.lease_deposit is null or t.lease_deposit = '' then '0' else t.lease_deposit end AS zlyj,
                                                    case when t.coast_electricity_deposit is null or t.coast_electricity_deposit = '' then '0' else t.coast_electricity_deposit end AS sdyj,
                                                    case when t.earnest_money is null or t.earnest_money = '' then '0' else t.earnest_money end AS bzj,
                                                    case when t.operating_area is null or t.operating_area = '' then '' else t.operating_area end as operating,
                                                    case when t.business_type is null or t.business_type = '' then '' else t.business_type end as business_type,
                                                    case when t.deposit_summary is null or t.deposit_summary = '' then '0' else t.deposit_summary end AS deposit_summary,
                                                    case when t.retreat_deposit_reason is null or t.retreat_deposit_reason = '' then '' else t.retreat_deposit_reason end as retreat_deposit_reason,
                                                    
                                                    case when t.tenant_hometown is null or t.tenant_hometown = '' then '' else t.tenant_hometown end as tenant_hometown,
                                                    case when t.tenant_address is null or t.tenant_address = '' then '' else t.tenant_address end as tenant_address,
                                                    case when t.carno is null or t.carno = '' then '' else t.carno end as carno,
                                                    case when cx.rule_name is null or cx.rule_name = '' then '' else cx.rule_name end as vehicle_model,
                                                    case when t.toll_standard is null or t.toll_standard = '' then '' else t.toll_standard end as toll_standard,
                                                    case when t.driver_license_name is null or t.driver_license_name = '' then '' else t.driver_license_name end as driver_license_name,
                                                    case when t.driver_license_number is null or t.driver_license_number = '' then '' else t.driver_license_number end as driver_license_number,
                                                    case when t.driving_license_name is null or t.driving_license_name = '' then '' else t.driving_license_name end as driving_license_name,
                                                    case when t.driving_license_number is null or t.driving_license_number = '' then '' else t.driving_license_number end as driving_license_number,
                                                    case when t.handledby is null or t.handledby = '' then '' else t.handledby end as handledby,
                                                    case when t.builder is null or t.builder = '' then '' else t.builder end as builder,
                                                    case when t.identity_number is null or t.identity_number = '' then '' else t.identity_number end as identity_number,
                                                    case when c.tablename is null or c.tablename = '' then '' else c.tablename end as valid_documents,
                                                    case when t.construction_time is null or t.construction_time = '' then '' else DATE_FORMAT(t.construction_time,'%Y年%m月%d日') end as construction_time,
                                                    case when t.construction_time_end is null or t.construction_time_end = '' then '' else DATE_FORMAT(t.construction_time_end,'%Y年%m月%d日') end as construction_time_end,
                                                    case when t.construction_unit is null or t.construction_unit = '' then '' else t.construction_unit end as construction_unit  ,
                                                    case when t.fire is null or t.fire = '' then '' else if(t.fire='1','是','否') end as fire  ,
                                                    case when t.decoration_deposit is null or t.decoration_deposit = '' then '' else t.decoration_deposit end as decoration_deposit,
                                                    case when c1.tablename is null or c1.tablename = '' then '' else c1.tablename end as choice_security_deposit,
                                                    case when t.old_tenant is null or t.old_tenant = '' then '' else t.old_tenant end as old_tenant,
                                                    case when t.renamed_money is null or t.renamed_money = '' then '' else t.renamed_money end as renamed_money,  
                                                    case when t.handling_department is null or t.handling_department = '' then '' else t.handling_department end as handling_department, 
                                                    case when t.batch_operating_area is null or t.batch_operating_area = '' then '' else t.batch_operating_area end as batch_operating_area,    
                                                    case when t.stalls_subtotal is null or t.stalls_subtotal = '' then '' else t.stalls_subtotal end as stalls_subtotal, 
                                                    case when t.contract_content is null or t.contract_content = '' then '' else t.contract_content end as contract_content,
                                                    case when t.earnest_money is null or t.earnest_money = '' then '' else t.earnest_money end as earnest_money,
                                                    case when c2.tablename is null or c2.tablename = '' then '' else c2.tablename end as choice_other_fee,
                                                    case when t.other_fee is null or t.other_fee = '' then '' else t.other_fee end as other_fee,  
                                                    case when t.sewage_charges is null or t.sewage_charges = '' then '' else t.sewage_charges end as sewage_charges,
                                                    case when t.electricity_service_charge is null or t.electricity_service_charge = '' then '' else t.electricity_service_charge end as electricity_service_charge,
                                                    case when t.water_fee is null or t.water_fee = '' then '' else t.water_fee end as water_fee,
                                                    case when t.electricity_bill is null or t.electricity_bill = '' then '' else t.electricity_bill end as electricity_bill,
                                                    case when t.water_ele_period_beg is null or t.water_ele_period_beg = '' then '' else DATE_FORMAT(t.water_ele_period_beg,'%Y年%m月%d日') end as water_ele_period_beg,
                                                    case when t.water_ele_period_end is null or t.water_ele_period_end = '' then '' else DATE_FORMAT(t.water_ele_period_end,'%Y年%m月%d日') end as water_ele_period_end
                                                    
                                                    
                                            from tabcwo_business_info t 
                                            left join staff s on s.staff_id = t.acceptor
                                            left join truck_cost_rules cx on t.vehicle_model=cx.id
                                            left join rolegroup r on s.defaultrolegroup = r.rolegroup_id
                                            left join c_codetable c1 on c1.IDDESC = t.choice_security_deposit and c1.id = '8'
                                            left join c_codetable c on c.IDDESC = t.valid_documents and c.id = '10'
                                            left join c_codetable c2 on c2.IDDESC = t.choice_other_fee and c2.id = '9' ''' + "\n" +
                    " where relation_id = '" + in_objective_guid + "' limit 1 ";
            log.debug("执行任务数据查询SQL:" + queryContractInfoSQL);
            List ContractInfo = eliteBean.chaxun_sql(queryContractInfoSQL, "dataSource");
            //获取总行数
            log.debug("执行合同信息查询数据SQL获取总条数:" + ContractInfo.size());

            if(ContractInfo.size()>0)
            {
                for (int i = 0; i < ContractInfo.size(); i++) {
                    Map selectResultcm = ContractInfo.get(i);
                    log.debug("************:"+selectResultcm);
                    String acceptor = selectResultcm.get("acceptor");//受理人
                    String rolegroupdescription = selectResultcm.get("rolegroupdescription");//受理组别
                    String createddate = selectResultcm.get("createddate");//受理时间
                    String writedate = selectResultcm.get("writedate");//填表日期
                        String htcbr = selectResultcm.get("htcbr");//合同承办人员
                    String cbrdh = selectResultcm.get("cbrdh");//承办人联系电话
                    String contract_name = selectResultcm.get("contract_name");//合同名称
                    String lease_term = selectResultcm.get("lease_term");//租赁期限开始
                    String lease_term_end = selectResultcm.get("lease_term_end");//租赁期限结束
                    String tenant = selectResultcm.get("tenant");//承租人姓名
                    String tenant_mobile = selectResultcm.get("tenant_mobile");//承租人联系电话
                    String tenant_code = selectResultcm.get("tenant_code");//承租人身份证号
                    String stalls_code = selectResultcm.get("stalls_code");//档位号
                    String area = selectResultcm.get("area");//面积
                    String myzj = selectResultcm.get("myzj");//每月租金
                    String myglf = selectResultcm.get("myglf");//每月管理费
                    String zlyj = selectResultcm.get("zlyj");//租赁押金
                    String sdyj = selectResultcm.get("sdyj");//水电押金
                    String bzj = selectResultcm.get("bzj");//保证金
                    String operating = selectResultcm.get("operating");//经营区域
                    String business_type = selectResultcm.get("business_type");//经营项目|经营种类
                    String deposit_summary = selectResultcm.get("deposit_summary");//押金汇总
                    String retreat_deposit_reason = selectResultcm.get("retreat_deposit_reason");//退押原因
                    String tenant_hometown = selectResultcm.get("tenant_hometown");//籍贯
                    String tenant_address = selectResultcm.get("tenant_address");//地址
                    String carno = selectResultcm.get("carno");//车牌号码
                    String vehicle_model = selectResultcm.get("vehicle_model");//车型
                    String toll_standard = selectResultcm.get("toll_standard");//收费标准
                    String driver_license_name = selectResultcm.get("driver_license_name");//驾驶证姓名
                    String driver_license_number=selectResultcm.get("driver_license_number");//驾驶证号
                    String driving_license_name = selectResultcm.get("driving_license_name");//行驶证姓名
                    String driving_license_number = selectResultcm.get("driving_license_number");//行驶证号
                    String jbr = selectResultcm.get("handledby");//经办人
                    //20211116Tang改
                    String builder = selectResultcm.get("builder"); //施工人
                    String identity_number = selectResultcm.get("identity_number"); //施工人证件号码
                    String valid_documents = selectResultcm.get("valid_documents"); //施工人有效证件
                    String construction_time = selectResultcm.get("construction_time"); //施工开始时间
                    String construction_time_end = selectResultcm.get("construction_time_end"); //施工结束时间
                    String construction_unit = selectResultcm.get("construction_unit"); //施工结束时间
                    String fire = selectResultcm.get("fire"); //是否动火
                    String decoration_deposit = selectResultcm.get("decoration_deposit"); //装修押金
                    String choice_security_deposit = selectResultcm.get("choice_security_deposit"); //保证金类型
                    String old_tenant = selectResultcm.get("old_tenant"); //原承租人
                    String renamed_money = selectResultcm.get("renamed_money"); //更名费
                    String handling_department = selectResultcm.get("handling_department"); //批量合同-经办部门
                    String batch_operating_area = selectResultcm.get("batch_operating_area"); //批量合同-经营区域
                    String stalls_subtotal = selectResultcm.get("stalls_subtotal"); //批量合同-合计
                    String contract_content = selectResultcm.get("contract_content");//批量合同-合同主要内容
                    String earnest_money = selectResultcm.get("earnest_money");//费用信息-保证金
                    String choice_other_fee = selectResultcm.get("choice_other_fee");//费用信息-选择其他费用
                    String other_fee = selectResultcm.get("other_fee");//费用信息-其他费用
                    String sewage_charges = selectResultcm.get("sewage_charges");//费用信息-排污费
                    String electricity_service_charge = selectResultcm.get("electricity_service_charge"); //供电服务费
                    String water_fee = selectResultcm.get("water_fee");
                    String electricity_bill = selectResultcm.get("electricity_bill");
                    String water_ele_period_beg = selectResultcm.get("water_ele_period_beg");
                    String water_ele_period_end = selectResultcm.get("water_ele_period_end");


                    ReHtml = ReHtml.replaceAll("ContractTitle",ContractTitle);
                    ReHtml = ReHtml.replaceAll("tenant_mobile",tenant_mobile);
                    ReHtml = ReHtml.replaceAll("tenant_code",tenant_code);
                    ReHtml = ReHtml.replaceAll("tenant",tenant);
                    ReHtml = ReHtml.replaceAll("lease_term_end",lease_term_end);
                    ReHtml = ReHtml.replaceAll("lease_term",lease_term);
                    ReHtml = ReHtml.replaceAll("acceptor",acceptor);
                    ReHtml = ReHtml.replaceAll("rolegroupdescription",rolegroupdescription);
                    ReHtml = ReHtml.replaceAll("createddate",createddate);
                    ReHtml = ReHtml.replaceAll("writedate",writedate);
                    ReHtml = ReHtml.replaceAll("htcbr",htcbr);
                    ReHtml = ReHtml.replaceAll("cbrdh",cbrdh);
                    ReHtml = ReHtml.replaceAll("contract_name",contract_name);
                    ReHtml = ReHtml.replaceAll("stalls_code",stalls_code);
                    ReHtml = ReHtml.replaceAll("area",area);
                    ReHtml = ReHtml.replaceAll("myzj",myzj);
                    ReHtml = ReHtml.replaceAll("myglf",myglf);
                    ReHtml = ReHtml.replaceAll("zlyj",zlyj);
                    ReHtml = ReHtml.replaceAll("sdyj",sdyj);
                    ReHtml = ReHtml.replaceAll("bzj",bzj);
                    ReHtml = ReHtml.replaceAll("operating",operating);
                    ReHtml = ReHtml.replaceAll("business_type",business_type);
                    ReHtml = ReHtml.replaceAll("deposit_summary",deposit_summary);
                    ReHtml = ReHtml.replaceAll("retreat_deposit_reason",retreat_deposit_reason);
                    ReHtml = ReHtml.replaceAll("jigguan",tenant_hometown);
                    ReHtml = ReHtml.replaceAll("dizhi",tenant_address);
                    ReHtml = ReHtml.replaceAll("carid",carno);
                    ReHtml = ReHtml.replaceAll("clxh",vehicle_model);
                    ReHtml = ReHtml.replaceAll("sfbz",toll_standard);
                    ReHtml = ReHtml.replaceAll("xsz_name",driving_license_name);
                    ReHtml = ReHtml.replaceAll("xsz_number",driving_license_number);
                    ReHtml = ReHtml.replaceAll("jsz_name",driver_license_name);
                    ReHtml = ReHtml.replaceAll("jsz_number",driver_license_number);
                    ReHtml = ReHtml.replaceAll("choice_security_deposit",choice_security_deposit);

                    ReHtml = ReHtml.replaceAll("jbr",jbr);
                    // 20211116Tang改
                    ReHtml = ReHtml.replaceAll("builder",builder);
                    ReHtml = ReHtml.replaceAll("identity_number",identity_number);
                    ReHtml = ReHtml.replaceAll("valid_documents",valid_documents);
                    ReHtml = ReHtml.replaceAll("construction_time",construction_time);
                    ReHtml = ReHtml.replaceAll("construc_time_end",construction_time_end);
                    ReHtml = ReHtml.replaceAll("construction_unit",construction_unit);
                    ReHtml = ReHtml.replaceAll("fire",fire);
                    ReHtml = ReHtml.replaceAll("decoration_deposit",decoration_deposit);
                    ReHtml = ReHtml.replaceAll("old_tena",old_tenant);
                    ReHtml = ReHtml.replaceAll("renamed_money",renamed_money);
                    ReHtml = ReHtml.replaceAll("handling_department",handling_department);
                    ReHtml = ReHtml.replaceAll("batch_oper_are_all",batch_operating_area);
                    ReHtml = ReHtml.replaceAll("stalls_subtotal",stalls_subtotal);
                    ReHtml = ReHtml.replaceAll("contract_content",contract_content);
                    ReHtml = ReHtml.replaceAll("earnest_money",earnest_money);
                    ReHtml = ReHtml.replaceAll("choice_other_fee",choice_other_fee);
                    ReHtml = ReHtml.replaceAll("other_fe",other_fee);
                    ReHtml = ReHtml.replaceAll("sewage_charges",sewage_charges);
                    ReHtml = ReHtml.replaceAll("electricity_service_charge",electricity_service_charge);
                    ReHtml = ReHtml.replaceAll("water_fee",water_fee);
                    ReHtml = ReHtml.replaceAll("electricity_bill",electricity_bill);
                    ReHtml = ReHtml.replaceAll("water_ele_period_beg",water_ele_period_beg);
                    ReHtml = ReHtml.replaceAll("water_ele_period_end",water_ele_period_end);

                }
                log.debug("*******************************")
                Calendar calendar= Calendar.getInstance();
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMdd");

                String PathSource = "/data/apache-tomcat-dabc/webapps/ngsapitask/print/html/";
                String fileNo = Util.getGuid();
                log.debug("******文件路径："+ PathSource);
                String FileNmae = ContractTitle + dateFormat.format(calendar.getTime()).toString() + fileNo + ".html";
                log.debug("******文件名："+ FileNmae);
                String DealFileName = PathSource + FileNmae
                log.debug("存储文件名：" + DealFileName);
                String text = ReHtml.toString()
                def tempFile = new File(DealFileName);
                tempFile.withWriter("UTF8") { writer ->
                    writer.write(text);
                }

                // 调用API打印PDF
                log.debug("开始处理打印pdf：");
                String re_msg = "";
                String re_pdfconvPath = "";
                String re_markPath = "";
                String waitDealPath = KHDBConfig.getAPIURL(eliteBean, "NGS-Local")+"/ngsapitask/print/html/" + FileNmae;
                log.debug("打印PDFAPI:" + waitDealPath);
                String callPDFJson = '''
                                        {
                                         "ismerge":"0",
                                         "ismark":"1",
                                         "markinfo":{
                                            "markpic":"/data/apache-tomcat-dabc/webapps/ngsapitask/print/ComLogo.png",
                                            "x_axis":400,
                                            "y_axis":100,
                                            "zoom":20
                                         },
                                         "value":[
                                            {
                                                "id":"111",''' + '\n' +
                        ''' "url":"''' + waitDealPath + '"\n' +
                        '''
                                            }
                                         ]
                                        }
                                        '''
                log.debug("PDF处理报文拼接：" + callPDFJson);
                String woPDFDealURL = KHDBConfig.getAPIURL(eliteBean, "NGS-Local") + "/ngsapitask/api/apiPDFDeal";
                String woPDFDealResult = Util.doHttpPost(woPDFDealURL, callPDFJson, "UTF-8", 5000, 0);
                JSONObject woPDFDealJson = JSONObject.parseObject(woPDFDealResult);
                log.debug("PDF处理结果返回：" + woPDFDealJson);
                String re_code = woPDFDealJson.getString("code");
                String re_value = woPDFDealJson.getString("value");
                def listValue = new JsonSlurper().parseText( re_value )

                if(re_code.equals("1"))
                {
                    re_msg = "PDF打印成功";
                    String re_pdfconvPathTemp = listValue.convPath;
                    if(re_pdfconvPathTemp.indexOf("/data/apache-tomcat-dabc/webapps/")>0)
                    {
                        re_pdfconvPath = KHDBConfig.getAPIURL(eliteBean, "NGSDOM") +re_pdfconvPathTemp.substring(re_pdfconvPathTemp.indexOf("webapps/")+8,re_pdfconvPathTemp.length()-1)

                    }
                    log.debug("处理结果："+re_pdfconvPath);
                    String re_markPathTemp = listValue.markPath;
                    if(re_markPathTemp.indexOf("/data/apache-tomcat-dabc/webapps/")>0)
                    {
                        re_markPath = KHDBConfig.getAPIURL(eliteBean, "NGSDOM") +re_markPathTemp.substring(re_markPathTemp.indexOf("webapps/")+8,re_markPathTemp.length()-1)

                    }
                    log.debug("处理结果："+re_markPath);

                }else
                {
                    re_msg = "PDF打印失败";
                    re_pdfconvPath = null;
                    re_markPath = null;

                };

                long endTime = System.currentTimeMillis();

                data.put("re_pdfconvPath", re_pdfconvPath );
                data.put("re_markPath", re_markPath);

                result.put("code", 1);
                result.put("msg", re_msg + "，处理时长：" + (endTime - startTime));
                result.put("data", data);

            }else
            {
                data.put("re_pdfconvPath", "" );
                data.put("re_markPath", "");

                result.put("code", 1009);
                result.put("msg", "工单数据查询为空，请确认！");
                result.put("data", data);

            }


        } catch (Exception e) {
            data.put("re_pdfconvPath", null );
            data.put("re_markPath", null);

            result.put("code", 1008);
            result.put("msg", e.getMessage());
            result.put("data", data);
        }

        return result.toString();

    }
}
